/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using ELE.EnterpriseLibrary.Data.Configuration;
using ELE.EnterpriseLibrary.Properties;

namespace ELE.EnterpriseLibrary.Data
{
  /// <summary>
  /// A view of object storage configuration data.
  /// </summary>
  public class ObjectStorageConfigurationView
  {
    private IConfigurationSource configurationSource;
    /// <summary>
    /// Initializes the view from a configuration source.
    /// </summary>
    /// <param name="configuration"></param>
    public ObjectStorageConfigurationView(IConfigurationSource configuration)
    {
      this.configurationSource = configuration;
    }
    /// <summary>
    /// Get storage data by name.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public ObjectStorageData GetStorageData(string name)
    {
      ObjectStorageSettings settings = this.ObjectStorageSettings;
      if (!settings.Storages.Contains(name))
      {
        throw new ConfigurationErrorsException(string.Format(Resources.Culture, Resources.ExceptionStorageNotFound, name));
      }
      return settings.Storages.Get(name);
    }
    /// <summary>
    /// Gets the name of the default <see cref="ObjectStorageData"/>.
    /// </summary>
    /// <returns>
    /// The name of the default <see cref="ObjectStorageData"/>.
    /// </returns>
    public string DefaultStorage
    {
      get
      {
        ObjectStorageSettings configSettings = this.ObjectStorageSettings;
        return configSettings.DefaultStorage;
      }
    }

    /// <summary>
    /// Gets the <see cref="ObjectStorageSettings"/> configuration data.
    /// </summary>
    /// <returns>
    /// The <see cref="ObjectStorageSettings"/> configuration data.
    /// </returns>
    public ObjectStorageSettings ObjectStorageSettings
    {
      get { return (ObjectStorageSettings)configurationSource.GetSection(ObjectStorageSettings.SectionName); }
    }
  }
}
